package com.returnsoft.resource.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.DepartmentDto;
import com.returnsoft.resource.eao.DepartmentEao;
import com.returnsoft.resource.entity.Department;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class DepartmentBean
 */
@Stateless
@LocalBean
public class DepartmentBean implements DepartmentInterface {

	@EJB
	private DepartmentEao departmentEao;
	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public DepartmentBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<DepartmentDto> findByCountry(Integer countryId)
			throws RecruitmentException {
		try {
			List<Department> departments = departmentEao
					.findByCountry(countryId);
			List<DepartmentDto> departmentsDto = new ArrayList<DepartmentDto>();
			for (Department department : departments) {
				DepartmentDto departmentDto = conv.fromDepartment(department);
				departmentsDto.add(departmentDto);
			}
			return departmentsDto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
